package com.huike.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.dto.ContractDto;
import com.huike.dto.domain.Course;
import com.huike.mapper.ContractMapper;
import com.huike.pojo.Contract;
import com.huike.service.ContractService;
import com.huike.service.CourseService;
import com.huike.utils.SecurityUtils;
import com.huike.vo.ContractVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {

    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private CourseService courseService;


    /*合同分页
     * */
    @Override
    public List<Contract> selectContractListPage(ContractDto contractDto) {
        List<Contract> contractList = contractMapper.selectContractList(contractDto);

        return contractList;
    }

    /*合同新增
     * */
    @Override
    public int addContract(Contract contract) {
        Contract cont = new Contract();
        //设置其他属性
        cont.setCreateTime(LocalDateTime.now());
        cont.setCreateBy(SecurityUtils.getUsername());
        cont.setDeptId(SecurityUtils.getDeptId());

        return super.save(contract) ? 1:0;
    }

    /*根据id获取合同
    * */
    @Override
    public Contract selectContractById(Long id) {
        Contract contract = contractMapper.getContract(id);
        return contract;
    }

    /*更新合同信息
    * */
    @Override
    public int updateContract(Contract contract) {
        return super.updateById(contract) ? 1 : 0;
    }

    /*获取详情
    * */
    @Override
    public ContractVo getallByid(Long id) {
        Contract contract = getById(id);
        ContractVo contractVo = new ContractVo();
        BeanUtils.copyProperties(contract,contractVo);
//        contractVo.setDiscountType(Contract.DEFAULT_DISCOUNT);
//        Long courseId = contract.getCourseId();
//        Course course = courseService.getById(courseId);
//        Integer price = course.getPrice();//课程购买价格
//        contractVo.setOrder(price);
//        contractVo.setCreateTime(LocalDateTime.now());
        return contractVo;
    }

    /*根据id删除合同
    * */
    @Override
    public int deletById(Long id) {
        boolean b = super.removeById(id);
        return b ? 1 : 0;
    }
}

